<!-- Buttons to add to the header. -->
<core-navbar-buttons slot="end">
    <ion-button fill="clear" *ngIf="showSearch" (click)="openSearch()" [ariaLabel]="'core.search' | translate">
        <ion-icon name="fas-magnifying-glass" slot="icon-only" aria-hidden="true" />
    </ion-button>

    @if (!courseContentsPage) {
    <ion-button fill="clear" (click)="openModuleSummary()" aria-haspopup="true" [ariaLabel]="'core.info' | translate">
        <ion-icon name="fas-circle-info" slot="icon-only" aria-hidden="true" />
    </ion-button>

    <!-- Add an empty context menu so split view pages can add items, otherwise the menu disappears in some cases. -->
    <core-context-menu />
    } @else {
    <core-context-menu>
        <core-context-menu-item [priority]="5000" [content]="'core.info' | translate" (action)="openModuleSummary()"
            iconAction="fas-circle-info" />
    </core-context-menu>
    }
</core-navbar-buttons>

<!-- Content. -->
<core-split-view>
    <ion-refresher slot="fixed" [disabled]="showLoading || (discussions && !discussions.loaded)" (ionRefresh)="doRefresh($event.target)">
        <ion-refresher-content pullingText="{{ 'core.pulltorefresh' | translate }}" />
    </ion-refresher>

    <core-loading [hideUntil]="!showLoading && discussions && discussions.loaded">
        <!-- Activity info. -->
        <core-course-module-info [module]="module" [description]="forum && forum.type !== 'single' && description" [component]="component"
            [componentId]="componentId" [courseId]="courseId" [hasDataToSync]="hasOffline || hasOfflineRatings"
            (completionChanged)="onCompletionChange()">
            <ion-item class="ion-text-wrap">
                <ion-label>
                    {{descriptionNote}}
                </ion-label>
            </ion-item>
        </core-course-module-info>

        <core-group-selector *ngIf="supportsChangeGroup" [groupInfo]="groupInfo" [(selected)]="groupId" (selectedChange)="groupChanged()"
            [courseId]="courseId" />

        <!-- Cut-off date or due date message -->
        <ion-card class="core-info-card" *ngIf="availabilityMessage">
            <ion-item>
                <ion-icon name="fas-circle-info" slot="start" aria-hidden="true" />
                <ion-label>{{ availabilityMessage }}</ion-label>
            </ion-item>
        </ion-card>

        <!-- Q&A message. -->
        <ion-card class="core-info-card" *ngIf="showQAMessage">
            <ion-item>
                <ion-icon name="fas-circle-info" slot="start" aria-hidden="true" />
                <ion-label>{{ 'addon.mod_forum.qandanotify' | translate }}</ion-label>
            </ion-item>
        </ion-card>

        <!-- Cannot add discussion to group messages. -->
        <ion-card class="core-info-card" *ngIf="usesGroups && canAddDiscussion && !canAddDiscussionToGroup">
            <ion-item>
                <ion-icon name="fas-circle-info" slot="start" aria-hidden="true" />
                <ion-label>
                    <span *ngIf="groupId <= 0">{{ 'addon.mod_forum.cannotadddiscussionall' | translate }}</span>
                    <span *ngIf="groupId > 0">{{ 'addon.mod_forum.cannotadddiscussion' | translate }}</span>
                </ion-label>
            </ion-item>
        </ion-card>

        <ng-container *ngIf="forum">
            <div *ngIf="discussions && sortingAvailable && selectedSortOrder" class="ion-text-wrap">
                <core-combobox [label]="('core.sort' | translate)" (onChange)="setSortOrder($event)" [selection]="selectedSortOrder.value">
                    <ion-select-option *ngFor="let order of sortOrders" class="ion-text-wrap" [value]="order.value">
                        {{ order.label | translate }}
                    </ion-select-option>
                </core-combobox>
            </div>

            <core-empty-box *ngIf="!discussions || (!discussions.hasDiscussions && !errorLoadingDiscussions)" icon="far-comments"
                [message]="'addon.mod_forum.forumnodiscussionsyet' | translate" />

            <core-empty-box *ngIf="errorLoadingDiscussions" icon="fas-triangle-exclamation"
                [message]="'addon.mod_forum.errorloadingsortingorder' | translate">
                <p>{{ 'addon.mod_forum.errorloadingsortingorderdetails' | translate }}</p>
            </core-empty-box>

            <ion-item *ngFor="let discussion of discussionsItems" class="addon-mod-forum-discussion" [detail]="true"
                [lines]="discussion.groupname && 'none'" [attr.aria-current]="discussions?.getItemAriaCurrent(discussion)"
                (click)="discussions?.select(discussion)" button>
                <ion-label>
                    <p class="addon-mod-forum-discussion-title ion-text-wrap item-heading">
                        <ion-icon name="fas-map-pin" *ngIf="discussion.pinned"
                            [attr.aria-label]="'addon.mod_forum.discussionpinned' | translate" />
                        <ion-icon name="fas-star" class="addon-forum-star" *ngIf="discussion.starred"
                            [attr.aria-label]="'addon.mod_forum.favourites' | translate" />
                        <core-format-text [text]="discussion.subject" contextLevel="module" [contextInstanceId]="module && module.id"
                            [courseId]="courseId" />
                        <ion-icon name="fas-lock" *ngIf="discussion.locked" class="addon-mod-forum-locked-icon"
                            [attr.aria-label]="'addon.mod_forum.discussionlocked' | translate" />
                    </p>
                    <div class="addon-mod-forum-discussion-info">
                        <core-user-avatar *ngIf="discussion.userfullname" [user]="discussion" slot="start" [courseId]="courseId"
                            [linkProfile]="false" />
                        <div class="addon-mod-forum-discussion-author">
                            <span *ngIf="discussion.userfullname" class="core-discussionusername">{{discussion.userfullname}}</span>
                            <p *ngIf="discussion.groupname" class="core-groupname">
                                <ion-icon name="fas-users" [attr.aria-label]="'addon.mod_forum.group' | translate" />
                                <core-format-text [text]="discussion.groupname" contextLevel="course" [contextInstanceId]="courseId"
                                    [wsNotFiltered]="true" />
                            </p>
                            <p *ngIf="isOnlineDiscussion(discussion)">
                                {{discussion.created * 1000 | coreFormatDate: "strftimerecentfull"}}
                            </p>
                            <p *ngIf="isOfflineDiscussion(discussion)">
                                <ion-icon name="fas-clock" aria-hidden="true" />
                                {{ 'core.notsent' | translate }}
                            </p>
                        </div>
                    </div>
                    <ion-row *ngIf="isOnlineDiscussion(discussion)" class="ion-text-center addon-mod-forum-discussion-more-info">
                        <ion-col class="ion-text-start">
                            <ion-note>
                                <ion-icon name="fas-clock" aria-hidden="true" /> {{ 'addon.mod_forum.lastpost' | translate }}
                                <ng-container *ngIf="discussion.timemodified > discussion.created">
                                    {{ discussion.timemodified | coreTimeAgo }}
                                </ng-container>
                                <ng-container *ngIf="discussion.timemodified <= discussion.created">
                                    {{ discussion.created | coreTimeAgo }}
                                </ng-container>
                            </ion-note>
                        </ion-col>
                        <ion-col class="ion-text-end">
                            <ion-note>
                                <ion-icon name="fas-comments" aria-hidden="true" />
                                {{ 'addon.mod_forum.numreplies' | translate:{numreplies: discussion.numreplies} }}
                                <ion-badge *ngIf="discussion.numunread" class="ion-text-center">
                                    <span aria-hidden="true">{{ discussion.numunread }}</span>
                                    <span class="sr-only">
                                        {{ 'addon.mod_forum.unreadpostsnumber' | translate:{ '$a' : discussion.numunread} }}
                                    </span>
                                </ion-badge>
                            </ion-note>
                        </ion-col>
                    </ion-row>
                </ion-label>
                <ion-button *ngIf="canPin || discussion.canlock || discussion.canfavourite" fill="clear"
                    [ariaLabel]="('core.displayoptions' | translate)" (click)="showOptionsMenu($event, discussion)" slot="end">
                    <ion-icon name="ellipsis-vertical" slot="icon-only" aria-hidden="true" />
                </ion-button>
            </ion-item>

            <core-infinite-loading [enabled]="discussions && discussions.loaded && !discussions.completed" [error]="fetchFailed"
                (action)="fetchMoreDiscussions($event)" />
        </ng-container>
    </core-loading>

    <core-course-module-navigation collapsible-footer [hidden]="showLoading" [courseId]="courseId" [currentModuleId]="module.id" />

    <ion-fab slot="fixed" core-fab vertical="bottom" horizontal="end" *ngIf="canAddDiscussionToGroup">
        <ion-fab-button (click)="openNewDiscussion()" [attr.aria-label]="addDiscussionText">
            <ion-icon name="fas-plus" aria-hidden="true" />
            <span class="sr-only">{{ addDiscussionText }}</span>
        </ion-fab-button>
    </ion-fab>
</core-split-view>
